#include "StatusGrpcClient.h"
#include "ConfigMgr.h"
#include "const.h"
StatusGrpcClient::StatusGrpcClient()
{
    auto& cfg = ConfigMgr::GetInstance();
    std::string host = cfg["StatusServer"]["Host"];
    std::string port = cfg["StatusServer"]["Port"];
    pool_ = std::make_unique<StatusConPool>(5,host,port);
}


message::LoginResponse StatusGrpcClient::Login(int uid,const std::string& token)
{
    auto stub = pool_->GetConnection();
    message::LoginResponse resp;
    if(!stub)
    {
        resp.set_error(ErrorCodes::RPCFailed);
        return resp;
    }
    //利用RAII机制，在函数结束时归还连接
    Defer defer([this,&stub](){
        pool_->ReturnConnection(std::move(stub));
    });
    message::LoginRequest req;
    req.set_uid(uid);
    req.set_token(token);
    ::grpc::ClientContext context;
    stub->Login(&context,req,&resp);
    return resp;
}
